'use strict';

angular.module('ProduceReady').controller('MOListController', ['$scope', 'BaseService', 'PublicModule', 'ProduceReadyEnum', 'PublicEnum', 'PublicFunction', '$location', 'Authentication',
    function($scope, BaseService, PublicModule, ProduceReadyEnum, PublicEnum, PublicFunction, $location, Authentication) {
        BaseService.getExtendEnum('ItemCategory', function(extendEnum){
            $scope.ItemCategoryEnum = extendEnum;
        });
        BaseService.getEnum('ProduceReadyEnum.MOStatus', function(enumData){
            $scope.MOStatusEnum = enumData;
        });

    	PublicModule.initModule($scope, 'MO');
        $scope.Module.MO.LoadMode = PublicEnum.LoadMode.CacheAndPaging;
        $scope.Module.MO.Populate = [{path: 'ItemMaster', select: 'Code Name'}];
        $scope.Module.MO.PageSize = 15;

        $scope.btnAdd = function(){
        	$location.url('/index/MO/Edit');
        };

        $scope.rowDblClick = function(){
            $scope.$apply(function(){
                $location.url("/index/MO/Edit/" + $scope.Module.MO.FocusedRecord._id);
            });
        };

        $scope.btnDelete = function(){
        	$scope.Module.MO.deleteSelecteds();
        };

        $scope.btnDownline = function(){
            var records = $scope.Module.MO.SelectedRecords;
            if(records.length == 0){
                PublicFunction.alert("没有选择下线记录", "下线提示");
                return;
            }
            var ids = [];
            for(var i=0; i<records.length; i++){
                if(records[i].Status == ProduceReadyEnum.MOStatus.Online.Value){
                    ids.push(records[i]._id);
                }
                else{
                    PublicFunction.alert("生产订单：" + records[i].Code + "不是开工状态，不能下线", "下线失败");
                    return;
                }
            }

            $scope.Module.MO.updateByValue({_id: {$in: ids}}, {Status: ProduceReadyEnum.MOStatus.Downline.Value}, function(){
                $scope.Module.MO.load();
            });
        };
    }
]);